﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;//定时器用的命名空间
using System.Threading.Tasks;
using Camera;//自定义的类库的父类
using HalconDotNet;

namespace CameraClassLibrary
{
    public class Dahua:AbstractCamera
    {
        /// <summary>
        /// 定时器
        /// </summary>
        Timer timer = null;

        private int _delay;//延时时间
        /// <summary>
        /// 延时
        /// </summary>
        public override int DelayTime 
        {
            get => _delay;
            set  
            {
                _delay = value;
                //改变定时器的间隔
                if (timer!=null)
                {
                    timer.Change(0, _delay);//延时时间
                }
            }
        }

        public Dahua()
        {
            BrandName = "大华";
        }

        public override void OpenCamera()
        {
            base.OpenCamera();
            //注册回调函数
            TimerCallback timerCallback = new TimerCallback(FunctionCallBack);
            //实例化定时器
            timer = new Timer(timerCallback, null, 0, DelayTime);
        }

        public void FunctionCallBack(object obj)
        {
            try
            {
                switch (CameraWorkState)
                {
                    case CamerWork.None:
                        break;
                    case CamerWork.采集一张:
                        //判断是否读取到了最后一张，如果读取到了最后一张那么就重头开始
                        if (Index < ImageURL.Length)
                        {
                            HOperatorSet.ReadImage(out Image, ImageURL[Index++]);
                        }
                        else
                        {
                            Index = 0;
                            HOperatorSet.ReadImage(out Image, ImageURL[Index++]);
                        }
                        HOperatorSet.DispObj(Image, HalconWindow);
                        CameraWorkState = CamerWork.None;
                        break;
                    case CamerWork.连续采集:
                        //判断是否读取到了最后一张，如果读取到了最后一张那么就重头开始
                        if (Index < ImageURL.Length)
                        {
                            HOperatorSet.ReadImage(out Image, ImageURL[Index++]);
                        }
                        else
                        {
                            Index = 0;
                            HOperatorSet.ReadImage(out Image, ImageURL[Index++]);
                        }
                        HOperatorSet.DispObj(Image, HalconWindow);
                        break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("[{0}] {1}", DateTime.Now, ex.Message);
            }
        }

        public override void CloseCamera()
        {
            base.CloseCamera();
            //清空显示控件
            HOperatorSet.ClearWindow(HalconWindow);
            //释放句柄
            HalconWindow.Dispose();
            //关闭定时器
            timer.Dispose();
        }

    }
}
